﻿package com.cn.mobileproject.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.util.Linkify;
import android.widget.TextView;

public class TextHelper {
    private static final String TAG = "TextHelper";
    
    private static HashMap<String, String> _userLinkMapping = new HashMap<String, String>();
    private static final Pattern NAME_MATCHER = Pattern.compile("@.+?\\s");
    private static final String TWITTA_USER_URL = "twitta://users/";
    private static final Pattern TAG_MATCHER = Pattern.compile("#\\w+#");
    private static Pattern USER_LINK = Pattern.compile("@<a href=\"http:\\/\\/fanfou\\.com\\/(.*?)\" class=\"former\">(.*?)<\\/a>");
    private static final String TWITTA_SEARCH_URL = "twitta://search/";
    protected static DecimalFormat numFormat = new DecimalFormat("#.#"); 
    
    public static String fileerMsgText(String text){
    	if(isEmpty(text))return "";
    		int index = text.indexOf("");
    	if(index!=-1){
    		return text.substring(0,index);
    	}
    	else return "";
    }
    
    public static String getFliteStr(String str){
		if(str==null||str.equals("null")){
			return "";
		}
		else return str.trim();
	}
    
    public static String getFileLengthUnitKB(long length){
    	return numFormat.format(length/1024.0)+"KB";
    }
    
    public static String changeCharset(String str, String oldCharset, String newCharset)  {
        if(str != null) {
            //用源字符编码解码字符串
            byte[] bs;
			try {
				bs = str.getBytes(oldCharset);
				return new String(bs, newCharset);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
        }
        return "";
    }
    
    /**
     * 判断字符串是否为空字符串和空指针
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0||s.equals("null")||"".equals(s.trim());
    }
    public static boolean isNotNull(String s) {
        return s == null ||s.equals("null");
    }

    /**
     * 把输入流里面的内容返回字符串
     */
    public static String stringifyStream(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();
    }

    private static final Linkify.MatchFilter NAME_MATCHER_MATCH_FILTER = new Linkify.MatchFilter() {
        @Override
        public final boolean acceptMatch(final CharSequence s, final int start,final int end) {
            String name = s.subSequence(start + 1, end).toString().trim();
            boolean result = _userLinkMapping.containsKey(name);
            return result;
        }
    };

    private static final Linkify.TransformFilter NAME_MATCHER_TRANSFORM_FILTER = new Linkify.TransformFilter() {
        @Override
        public String transformUrl(Matcher match, String url) {
            String name = url.subSequence(1, url.length()).toString().trim();
            return _userLinkMapping.get(name);
        }
    };
    
    
    public static boolean isNull(String s) {
        return s == null;
    }
    
    public static boolean isNullEmpty(String s) {
       return s == null || s.length() == 0;
   }


    public static void linkifyUsers(TextView view) {
        Linkify.addLinks(view, NAME_MATCHER, TWITTA_USER_URL,NAME_MATCHER_MATCH_FILTER, NAME_MATCHER_TRANSFORM_FILTER);
    }

    private static final Linkify.TransformFilter TAG_MATCHER_TRANSFORM_FILTER = new Linkify.TransformFilter() {
        @Override
        public final String transformUrl(Matcher match, String url) {
            String result = url.substring(1, url.length() - 1);
            return "%23" + result + "%23";
        }
    };

    public static void linkifyTags(TextView view) {
        Linkify.addLinks(view, TAG_MATCHER, TWITTA_SEARCH_URL, null,TAG_MATCHER_TRANSFORM_FILTER);
    }
    
    public static String getSimpleHtmlText(String text) {
        return text.replaceAll("<.*?>", "").replace("&lt;", "<")
                .replace("&gt;", ">").replace("&nbsp;", " ")
                .replace("&amp;", "&").replace("&quot;", "\"");
    }

}
